MODULE YOS_CMF_INPUT
!==========================================================
!* PURPOSE: Shared variables for CaMa-Flood model configulation
! (C) D.Yamazaki & E. Dutra  (U-Tokyo/FCUL)  Aug 2019
!

! Modified by Zhongwang Wei @ SYSU 2022.11.20: add water re-infiltration calculation 

! Licensed under the Apache License, Version 2.0 (the "License");
!   You may not use this file except in compliance with the License.
!   You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
!
! Unless required by applicable law or agreed to in writing, software distributed under the License is 
!  distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
! See the License for the specific language governing permissions and limitations under the License.
!==========================================================
USE PARKIND1,                ONLY: JPIM, JPRB, JPRM,JPRD
IMPLICIT NONE
SAVE 
!================================================
!*** CMF default files
LOGICAL                         :: LLOGOUT                 !! true: log output to file
INTEGER(KIND=JPIM)              :: LOGNAM                  !! default log    file FID
INTEGER(KIND=JPIM)              :: NSETFILE                !! input namelist file FID
INTEGER(KIND=JPIM)              :: TMPNAM                  !! temporal I/O   file FIG
CHARACTER(LEN=256)              :: CLOGOUT                 !! default log    file name
CHARACTER(LEN=256)              :: CSETFILE                !! input namelist file name

DATA LLOGOUT       /.TRUE./
DATA CLOGOUT       /'./log_CaMa.txt'/
DATA CSETFILE      /'../run/cama_flood.nml'/

!================================================
!*** NAMELIST/NRUNVER/
LOGICAL                         :: LADPSTP                 !! true: use adaptive time step

LOGICAL                         :: LFPLAIN                 !! true: consider floodplain (false: only river channel)
LOGICAL                         :: LKINE                   !! true: use kinematic wave
LOGICAL                         :: LFLDOUT                 !! true: floodplain flow (high-water channel flow) active
LOGICAL                         :: LPTHOUT                 !! true: activate bifurcation scheme
LOGICAL                         :: LDAMOUT                 !! true: activate dam operation (under development)
LOGICAL                         :: LDAMIRR                 !! true: activate dam irrigation scheme
LOGICAL                         :: LLEVEE                  !! true: activate levee scheme  (under development)
!~~ used in ECMWF
LOGICAL                         :: LROSPLIT                !! true: input if surface (Qs) and sub-surface (Qsb) runoff
LOGICAL                         :: LWEVAP                  !! true: input water evaporation to extract from floodplain
LOGICAL                         :: LWEVAPFIX               !! true: water balance closure extracting water from evap when available
LOGICAL                         :: LWINFILT                !! true: input water infiltration to extract from floodplain
LOGICAL                         :: LWINFILTFIX             !! true: water balance closure extracting water from Infiltration when available
LOGICAL                         :: LWEXTRACTRIV            !! true: also extract water from rivers 
LOGICAL                         :: LSLOPEMOUTH             !! true: prescribe water level slope == elevation slope on river month
LOGICAL                         :: LGDWDLY                 !! true: Activate ground water reservoir and delay
LOGICAL                         :: LSLPMIX                 !! true: activate mixed kinematic and local inertia based on slope

LOGICAL                         :: LMEANSL                 !! true : boundary condition for mean sea level
LOGICAL                         :: LSEALEV                 !! true : boundary condition for variable sea level

LOGICAL                         :: LOUTINS                 !! true: diagnose instantaneous discharge 

LOGICAL                         :: LRESTART                !! true: initial condition from restart file
LOGICAL                         :: LSTOONLY                !! true: storage only restart (mainly for data assimilation)

LOGICAL                         :: LOUTPUT                 !! true: use standard output (to file)
LOGICAL                         :: LOUTINI                 !! true: output initial storage (netCDF only)

LOGICAL                         :: LGRIDMAP                !! true: for standard XY gridded 2D map
LOGICAL                         :: LLEAPYR                 !! true: neglect leap year (Feb29 skipped)
LOGICAL                         :: LMAPEND                 !! true: for map data endian conversion
LOGICAL                         :: LBITSAFE                !! true: for Bit Identical (removed from v410, set in Mkinclude)
LOGICAL                         :: LSTG_ES                 !! true: for Vector Processor optimization (CMF_OPT_FLDSTG_ES) 
LOGICAL                         :: LSPAMAT                 !! true: quasi sparse matrix to avoid OMP_Atomic (small RAM use added) 

LOGICAL                         :: LTRACE                  !! true: tracer   scheme                 
LOGICAL                         :: LSEDIMENT               !! true: sediment scheme                 

!================================================
!*** NAMELIST/NCONF/
CHARACTER(LEN=256)              :: CDIMINFO                !! Dimention Information

REAL(KIND=JPRB)                 :: DT                      !! Time Step Length [SEC] (should be multiple of 60)

INTEGER(KIND=JPIM)              :: IFRQ_OUT                !! [hour]: frequency to write output     e.g. (1,2,3,6,12,24) hour
INTEGER(KIND=JPIM)              :: IFRQ_INP                !! [hour]: frequency to update runoff    e.g. (1,2,3,6,12,24) hour
INTEGER(KIND=JPIM)              :: IFRQ_SL                 !! [min]:  frequency to update sea level e.g. (1,2,5,10,15,20,30,60) min

!*** set by CDIMINFO
INTEGER(KIND=JPIM)              :: NX                      !! NUMBER OF GRIDS IN HORIZONTAL
INTEGER(KIND=JPIM)              :: NY                      !! NUMBER OF GRIDS IN VERTICAL
INTEGER(KIND=JPIM)              :: NLFP                    !! NUMBER OF VERTICAL LEVELS DEFINING FLOODPLAIN

INTEGER(KIND=JPIM)              :: NXIN                    !! NUMBER OF GRIDS IN HORIZONTAL
INTEGER(KIND=JPIM)              :: NYIN                    !! NUMBER OF GRIDS IN VERTICAL
INTEGER(KIND=JPIM)              :: INPN                    !! MAX INPUT NUMBER

REAL(KIND=JPRD)                 :: WEST                    !! west, east, north, south edge of the domain [deg]
REAL(KIND=JPRD)                 :: EAST
REAL(KIND=JPRD)                 :: NORTH
REAL(KIND=JPRD)                 :: SOUTH

!*** calculated from IFRQ & DT
REAL(KIND=JPRB)                 :: DTIN                    !! SECOND IN INPUT TIME STEP [SEC]
REAL(KIND=JPRB)                 :: DTSL                    !! SECOND IN TIME STEP [SEC]

!================================================
!*** NAMELIST/PARAM/
!* parameters
REAL(KIND=JPRB)                 :: PMANRIV              !! manning roughness (river)
REAL(KIND=JPRB)                 :: PMANFLD              !! manning roughness (floodplain)
REAL(KIND=JPRB)                 :: PGRV                 !! gravity acceleration [m/s2]
REAL(KIND=JPRB)                 :: PDSTMTH              !! downstream distance at river mouth [m]
REAL(KIND=JPRB)                 :: PCADP                !! CFL coefficient
REAL(KIND=JPRB)                 :: PMINSLP              !! minimum topographic slope (kinematic wave) [m/m]
!* missing values
INTEGER(KIND=JPIM)              :: IMIS                !! integer undefined
REAL(KIND=JPRM)                 :: RMIS                !! real    undefined
REAL(KIND=JPRB)                 :: DMIS                !! double  undefined
!* file suffix
CHARACTER(LEN=256)              :: CSUFBIN            ! .bin suffix for binary (2D map)
CHARACTER(LEN=256)              :: CSUFVEC            ! .vec suffix for binary (1D vector)
CHARACTER(LEN=256)              :: CSUFPTH            ! .pth suffix for binary (1D bifurcation channel)
CHARACTER(LEN=256)              :: CSUFCDF            ! .nc  suffix for netCDF
!================================================
#ifdef IFS_CMF
! Fluxes buffers for IFS coupling
REAL(KIND=JPRB), ALLOCATABLE :: ZBUFFO(:,:,:)
REAL(KIND=JPRB), ALLOCATABLE :: ZBUFFI(:,:,:)
REAL(KIND=JPRB), ALLOCATABLE :: ZACC0(:,:)
REAL(KIND=JPRB), ALLOCATABLE :: ZACC1(:,:)
!Time step to be advanced within DRV_ADVANCE used for IFS coupling
INTEGER(KIND=JPIM)           :: ISTEPADV
!===============================================
#endif

END MODULE YOS_CMF_INPUT
